#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 5;
int a[maxn], c[maxn], t[maxn], anss[maxn];
signed main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin.tie(0);cout.tie(0);
    int n, m;cin >> n >> m;
    for(int i = 1;i <= n;i ++)cin >> a[i];
    for(int i = 1;i <= m;i ++)cin >> c[i];
    int sum = 1, k;
    for(int i = 1;i <= 31;i ++){
        if(sum >= n)k = i - 1;
        sum *= 2;
    }
    for(int i = 1;i <= k;i ++){
        cin >> t[i];
    }
    int T;cin >> T;
    while(T --){
        int x1, x2, x3, x4;cin >> x1 >> x2 >> x3 >> x4;
        int ans = 0;
        for(int i = 1;i <= k;i ++){
            while(t[i]){
                int temp;
                temp = t[i] % 10;
                t[i] /= 10;
                if(temp){
                    if(a[n] < i){
                        anss[i] ++;
                    }
                }
                if(!temp){
                    if(a[i] < i){
                        anss[i] ++;
                    }
                }
            }
            ans ^= anss[i];
        }
        cout << ans << endl;
    }
}